
/**
 * First GUI
 * Gui for select to play singleplayer or multiplayer
 * (SinglePlayer) -- two player with 1 screen
 * (MultiPlayer) -- run by localnetwork
 */

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.net.ConnectException;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

import Client.Client;
import Client.MainClient;

public class FirstGUI {

	private JFrame frame;
	private JPanel [] panel;
	private JButton single,multi;
	private JTextField ipLabel,nameLabel;
	
	private MultiAction multiAction;
	private SingleAction singleAction;
	
	/**
	 * Conustructor 
	 * create JFrame, initComponent
	 */
	public FirstGUI()
	{
		frame = new JFrame("Matching Game");
		frame.setDefaultCloseOperation(3);
		frame.setPreferredSize(new Dimension (200,160));
		frame.setLocation(500,300);
		initComponent();
	}
	
	/**
	 * initComponent of JFrame
	 */
	public void initComponent()
	{
		panel = new JPanel[3];
		panel[0] = new JPanel();
		panel[1] = new JPanel();
		panel[2] = new JPanel();
		
		singleAction = new SingleAction(this);
		single = new JButton("SinglePlayer");
		single.setPreferredSize(new Dimension (150,50));
		single.setFont(new Font("Serif",0,15));
		single.addActionListener(singleAction);
		
		multiAction = new MultiAction(this);
		multi = new JButton("MultiPlayer");
		multi.setPreferredSize(new Dimension (150,50));
		multi.setFont(new Font("Serif",0,15));
		multi.addActionListener(multiAction);
		
		panel[0].add(single);
		panel[1].add(multi);
		frame.add(panel[0],BorderLayout.NORTH);
		frame.add(panel[1],BorderLayout.CENTER);
		
		
	}
	
	/**
	 * setVisible JFrame and pack
	 * @param visible 
	 */
	public void run(boolean visible)
	{
		frame.pack();
		frame.setVisible(visible);
	}
	

	/**
	 * method when click Multiplayer Button
	 * change some component for select "Host" or "Join" and add textfield "ipAddrese" 
	 */
	public void multi()
	{
		single.setText("Create Host Game");
		single.removeActionListener(singleAction);
		single.addActionListener(new HostAction(this));
		
		multi.setText("Join Game");
		multi.removeActionListener(multiAction);
		multi.addActionListener(new JoinAction(this));
		
		frame.setPreferredSize(new Dimension(200,200));
		frame.pack();
		
		panel[2] = new JPanel();
		panel[2].setLayout(new BorderLayout());
		
		ipLabel = new JTextField();
		ipLabel.setPreferredSize(new Dimension(80,40));
		ipLabel.setBorder(new TitledBorder("IP Address"));
		panel[2].add(ipLabel);
		
		/*
		nameLabel = new JTextField();
		nameLabel.setPreferredSize(new Dimension(80,40));
		nameLabel.setBorder(new TitledBorder("Player Name"));
		panel[2].add(nameLabel,BorderLayout.SOUTH);
		*/
		
		frame.add(panel[2],BorderLayout.SOUTH);
		frame.pack();
	}
	
	/**
	 * fucntion on click multiButton
	 * @author Termchai 5510546042, Eknarin 5510546239
	 *
	 */
	class MultiAction extends AbstractAction
	{

		FirstGUI fg;
		public MultiAction(FirstGUI fg)
		{
			this.fg=fg;
		}
		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			fg.multi();
		}
		
	}
	
	/**
	 * function on click singleButton
	 * @author Termchai 5510546042, Eknarin 5510546239
	 *
	 */
	class SingleAction extends AbstractAction
	{

		private FirstGUI fg;
		public SingleAction(FirstGUI fg)
		{
			this.fg=fg;
		}
		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			try {
				SinglePlayer.Main.main(new String[1]);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			fg.run(false);
		}
		
	}
	
	/**
	 * function on click hostButton
	 * @author Termchai 5510546042, Eknarin 5510546239
	 *
	 */
	class HostAction extends AbstractAction
	{

		private FirstGUI fg;
		public HostAction(FirstGUI fg)
		{
			this.fg=fg;
		}
		
		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			fg.run(false);
			server.MainServer.main(new String[1]);
			try {
				MainClient.main(new String[]{"127.0.0.1"});
			} catch (ConnectException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			JOptionPane.showMessageDialog(new JFrame("Create Server"),"Server Ready, Waiting another Player");
		}
		
	}
	
	/**
	 * function on click joinButton
	 * @author Termchai 5510546042, Eknarin 5510546239
	 *
	 */
	class JoinAction extends AbstractAction
	{
		private FirstGUI fg;
		public JoinAction(FirstGUI fg)
		{
			this.fg=fg;
		}
		
		@Override
		public void actionPerformed(ActionEvent e) {
			String[] ip = new String[1];
			ip[0] = ipLabel.getText();
			try {
				MainClient.main(ip);
			} catch (ConnectException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
			fg.run(false);
		}
		
	}
	
}
